//
//  ModifyUserNameController.m
//  YSSmartHome
//
//  Created by yanghao on 2017/8/9.
//  Copyright © 2017年 YangHao. All rights reserved.
//

#import "ModifyUserNameController.h"
#import "YSCommon.h"
#import "SureView.h"
#import "UIView+HUD.h"
#import "MQTTSessionManager.h"
#import <XAISDK/XAI.h>


@interface ModifyUserNameController ()
@property (nullable, nonatomic, strong) UITextField* usernameTextFiled;
@property (nullable, nonatomic, strong) SureView* mSureView;
@property (nullable, nonatomic, strong) MQTTSessionManager* sessionManager;
@end

@implementation ModifyUserNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
    __weak typeof(self)wself = self;
    self.mSureView.sureBtnClickBlock = ^{
        __strong typeof(wself)sself = wself;
        if (!sself) {
            return ;
        }
        if (sself.usernameTextFiled.text.length == 0) {
            [sself.view yh_showMsgHUD: @"名字不能为空"];
            return;
        }
        [sself.sessionManager.xai sendMyDeviceName:sself.usernameTextFiled.text];
        [sself.navigationController popViewControllerAnimated:true];
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private method
- (void)initSubviews {
    self.navigationItem.title = @"修改昵称";
    self.view.backgroundColor = kBgColor;
    [self.view addSubview:self.usernameTextFiled];
    [self.view addSubview:self.mSureView];
    [self layoutPageSubViews];
}

- (void)layoutPageSubViews {
    [self.usernameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view).offset(UIEdgeInsetsMake(10, 0, 0, 0));
        make.height.equalTo(kIphoneSize_Height(44));
    }];

    [self.mSureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.usernameTextFiled.bottom).offset(kIphoneSize_Height(60));
        make.height.equalTo(100);
    }];
}

#pragma mark - getters and stters

- (UITextField *)usernameTextFiled {
    if (!_usernameTextFiled) {
        _usernameTextFiled = [[UITextField alloc]init];
        _usernameTextFiled.placeholder = @"请输入新的昵称";
        _usernameTextFiled.backgroundColor = [UIColor whiteColor];
        UILabel* label = [[UILabel alloc]init];
        label.text = @"昵称";
        label.frame = CGRectMake(0, 0, 100, 44);
        label.textAlignment = NSTextAlignmentCenter;
        _usernameTextFiled.leftView = label;
        _usernameTextFiled.leftViewMode = UITextFieldViewModeAlways;
        _usernameTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _usernameTextFiled;
}

- (SureView *)mSureView {
    if (!_mSureView) {
        _mSureView = [[SureView alloc]init];
        _mSureView.title = @"确认";
    }
    return _mSureView;
}

- (MQTTSessionManager *)sessionManager{
    if (!_sessionManager) {
        _sessionManager = [MQTTSessionManager sharedManager];
    }
    return _sessionManager;
}
@end
